﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ImageProcessing.Utilities;
using ImageProcessing.IntermediateClasses;

namespace ImageProcessing
{
    public partial class ArithmeticOperations : Form
    {
        private int operation;
        private ArithmaticLogicOperations ALOperations = new ArithmaticLogicOperations();
        private PictureControl imageControl = new PictureControl();

        public ArithmeticOperations(int operation)
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
            this.operation = operation;
            if (this.operation == 0)
            {
                txtFraction.Enabled = true;
            }
            else
            {
                txtFraction.Enabled = false;
            }
        }

        private void btnBrowseImg1_Click(object sender, EventArgs e)
        {
            try
            {
                picBoxImg1.Image = this.imageControl.OpenImage();
            }
            catch
            {
                MessageBox.Show("Cannot open image!!", "Error");
            }
        }

        private void btnBrowseImg2_Click(object sender, EventArgs e)
        {
            try
            {
                picBoxImg2.Image = this.imageControl.OpenImage();
            }
            catch
            {
                MessageBox.Show("Cannot open image!!", "Error");
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            double fraction;
            try
            {
                if (this.operation == 0)
                {
                    if (txtFraction.Text.Equals(null))
                    {
                        MessageBox.Show("Please enter the fraction.", "Error");
                    }
                    else if (!(double.TryParse(txtFraction.Text, out fraction)))
                    {
                        MessageBox.Show("Please enter a fraction from 1 to 0", "Error");
                    }
                    else
                    {
                        picBoxResult.Image = this.ALOperations.Add(picBoxImg1.Image, picBoxImg2.Image, double.Parse(txtFraction.Text));
                        if (picBoxResult.Image.Equals(null))
                        {
                            throw new ArgumentNullException();
                        }
                    }
                }
                else if (this.operation == 1)
                {
                    picBoxResult.Image = picBoxResult.Image = this.ALOperations.Subtract(picBoxImg1.Image, picBoxImg2.Image);
                }
                else if (this.operation == 2)
                {
                    picBoxResult.Image = this.ALOperations.And(picBoxImg1.Image, picBoxImg2.Image);
                }
                else if (this.operation == 3)
                {
                    picBoxResult.Image = this.ALOperations.OR(picBoxImg1.Image, picBoxImg2.Image);
                }
            }
            catch
            {
                MessageBox.Show("Cannot complete operation!!", "Error");
            }
        }
    }
}
